﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BookReader
{
    class helpCommand:ICommand
    {
        void ICommand.Execute(string[] param)
        {
            if (param == null)
            {
                ICommand cmd = null;
                Type[] tps = Assembly.GetExecutingAssembly().GetTypes();
                List<Type> commandTypes = new List<Type>();
                foreach (Type tp in tps)
                {
                    if (tp.Name.EndsWith("Command"))
                    {
                        if (!tp.Name.Contains("ICommand"))
                            commandTypes.Add(tp);
                    }
                }
                Console.WriteLine(StaticData.Help);
                foreach (Type tp in commandTypes)
                {
                    cmd = (ICommand)Assembly.GetExecutingAssembly().CreateInstance(tp.FullName);
                    Console.WriteLine(cmd.Help());
                }
            }
            else
            {
                if (param.Length == 1)
                {
                    Type tp = Type.GetType("BookReader." + param[0] + "Command");
                    if (tp != null)
                    {
                        Console.WriteLine(((ICommand)Activator.CreateInstance(tp)).Help());
                    }
                }
            }
        }

        string ICommand.Help()
        {
            return "help命令：帮助命令 help 或者 help 命令名";
        }
    }
}
